package com.company.vmpx.ui.main;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.dee.components.base.ui.JumpOptions;

import com.dee.components.util.ActivityTManager;
import com.dee.components.widget.ClearableEditText;
import com.dee.components.widget.PasswordEditText;
import com.company.vmpx.R;
import com.company.vmpx.base.BaseActivity;


import com.company.vmpx.ui.main.entity.LoginEntity;

import com.company.vmpx.ui.main.contract.CLogin;
import com.company.vmpx.ui.main.presenter.PLogin;
import com.dee.components.util.ToastTool;
import com.dee.components.util.DataUtils;


import com.dee.components.widget.TitleView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Consumer;

/**
 * Created by Liu on 2020/4/19.
 * 当前类注释:
 * 登录
 */
public class LoginActivity extends BaseActivity<PLogin> implements CLogin.IView {

    @BindView(R.id.toolbar_title_view)
    TitleView toolbarTitleView;
    @BindView(R.id.login_mobile_et)
    ClearableEditText loginMobileEt;
    @BindView(R.id.login_ps_et)
    PasswordEditText loginPsEt;
    @BindView(R.id.login_remember_ps)
    TextView loginRememberPs;
    @BindView(R.id.login_bottpm_tip)
    TextView loginBottpmTip;

    public static JumpOptions startJump(String datatype) {
        return new JumpOptions()
                //.setBundle(AppConstant.INTENT_DATATYPE, datatype)
                ;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initPresenter() {
        //简单页面无需mvp就不用管此方法即可
        mPresenter.setVM(this);
    }

    private boolean rememberPs = false;

    @Override
    public void initView(Bundle savedInstanceState) {

        //初始化TitleView
        bindTitleView();


    }

    @OnClick({R.id.login_btn, R.id.login_remember_ps, R.id.login_content_ll})
    public void onViewClicked(View view) {
        if (antiShake.check(view.getId())) return;
        switch (view.getId()) {
            case R.id.login_remember_ps:
                rememberPs = !rememberPs;
                loginRememberPs.setSelected(rememberPs);
                break;
            case R.id.login_btn:
                String mobile = loginMobileEt.getText().toString();
                String ps = loginPsEt.getText().toString();
                if(DataUtils.isNullString(mobile)){
                    ToastTool.normal(R.string.input_phone_tips);
                    return;
                }
                if(DataUtils.isNullString(ps)){
                    ToastTool.normal(R.string.input_pw_tips);
                    return;
                }

                startActivityFinish(MainActivity.class);
                break;
            case R.id.login_content_ll:
                break;
        }
    }

    // 初始化TitleView
    private void bindTitleView() {
        toolbarTitleView.setTitle(R.string.title_login);
        toolbarTitleView.setLeftIconVisibility(false);
        toolbarTitleView.setLeftTextVisibility(false);

    }


    @Override
    public void resultData(LoginEntity data) {

    }

    @Override
    public void onErrorSuccess(int code, String message, boolean isSuccess, boolean showTips) {

        if (!isSuccess) {
            ToastTool.error(message);
        } else if (showTips && !DataUtils.isNullString(message)) {
            ToastTool.success(message);
        }
    }

    // 用来判断 两次返回键退出app
    private boolean isExit = false;

    @Override
    public void onBackPressed() {
        if (!isExit) {
            isExit = true;
            ToastTool.normal("再按一次退出程序");
            mRxManager.add(Observable.timer(2000, TimeUnit.MILLISECONDS)
                    .subscribe(new Consumer<Long>() {
                        @Override
                        public void accept(Long aLong) throws Exception {
                            isExit = false;
                        }
                    }));
            return;
        }
        ActivityTManager.get().AppExit(context, false);
    }

}
